package com.biz.hunter.service.impl;

import com.biz.hunter.dao.EmployeeDAO;
import com.biz.hunter.dao.RecommendDAO;
import com.biz.hunter.db.entity.*;
import com.biz.hunter.service.IRecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RecommendService implements IRecommendService {
	@Autowired
	RecommendDAO recommendDAO;

	@Autowired
	EmployeeDAO empDAO;

	@Override
	public Recommendation getRecommendationByID(Integer id) {
		Recommendation rd = recommendDAO.getById(id.toString());

		return rd;
	}

	@Override
	public List<Recommendation> getAllRecommendation() {

		return recommendDAO.getALL();
	}

	@Override
	public int createRecommendation(Recommendation recommend) {
		return recommendDAO.save(recommend);
	}

	@Override
	public void deleteById(Integer Id) {
		recommendDAO.delete(Id.toString());

	}

	@Override
	public void updateRecommendation(Recommendation recommend) {
		recommendDAO.update(recommend);
	}

	@Override
	public void updateRecommendationResource(Recommendation recommend) {
		recommendDAO.updateRes(recommend);
	}
	
	@Override
	public void updateRecommendationAudioResource(Recommendation recommend) {
		recommendDAO.updateAudioRes(recommend);
	}
	
	@Override
	public List<Recommendation> getRecommendationsByCreater(Integer personId, int start) {
		List<Recommendation>  list = recommendDAO.getByCreater(personId, start);

		return list;

	}

	@Override
	public List<RecommendPosition> getRecommendPositionByCreater(int platformid, Integer personId, Integer refereeType,int start) {
		List<RecommendPosition>  list = recommendDAO.getRecommendPositionByCreater(platformid, personId,refereeType, start);

		return list;
	}

	@Override
	public List<RecommendedPerson> getRecommendedPersonByPositionAndCreater(int platformid, Integer personId, Integer refereeType,Integer jobId, int start) {
		List<RecommendedPerson>  list = recommendDAO.getRecommendedPersonByPositionAndCreater(platformid, personId, refereeType,jobId, start);

		return list;
	}

	@Override
	public List<RecommendedPerson> getRecommendedPersonByPositionAndCreater2(int platformid, Integer personId, Integer jobId, int start) {
		List<RecommendedPerson>  list = recommendDAO.getRecommendedPersonByPositionAndCreater2(platformid, personId, jobId, start);

		return list;
	}

	@Override
	public List<Recommendation> getSelfRecommendations(Integer personId,int offset) {
		List<Recommendation>  list = recommendDAO.getByCreater(personId, offset);

		return list;
	}

	@Override
	public List<SelfRecommendPosition> getSelfRecommendPositionByCreater(int personId, int start) {
		List<SelfRecommendPosition>  list = recommendDAO.getSelfRecommendPositionByCreater(1, personId, start);

		return list;
	}

	@Override
	public List<Recommendation> searchRecommenbyJobandPerson(Integer jobId, String openId, int platformid) {
		Employee emp = empDAO.getByOpenId(openId);//group by job id TODO
		List<Recommendation> list = null;
		if (emp == null) {
			return null;
		}
		int personId = emp.getPersonId();
	    list = recommendDAO.getByJobandPerson(jobId.intValue(), personId, platformid);

		return list;
	}
	
	@Override
	public boolean isAlreadySelfRecommended(int jobId, int personId) {
		List<Recommendation> list = null;
		list = recommendDAO.getByJobandPerson(jobId, personId, 1);
		
		if(list.size() > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	@Override
	public boolean isAlreadyRecommended(int jobId, String mobile, int platformid) {
		List<Recommendation> list = null;
		list = recommendDAO.getByJobAndPhone(jobId, mobile, platformid);
		if(list.size() >0 ) {
			return true;
		} else {
			return false;
		}
	}

}
